package com.sun.filter;

import com.sun.util.Constant;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Sun
 * @Date 2020/4/23 21:02
 * @Version 1.0
 */
public class SysFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;//必须向下转型，才能获得session对象
        HttpServletResponse resp = (HttpServletResponse) response;//必须向下转型，才有重定向方法

       if((req.getSession().getAttribute(Constant.USER_SESSION))==null) {
           resp.sendRedirect("/index.jsp");//过滤掉属性里没有sessionID的请求，以免直接访问成功页面
       }

       chain.doFilter(request, response);
    }

    public void destroy() {

    }
}
